<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/6/3
 * Time: 15:01
 */
namespace Admin\Controller;
use Think\Controller;

class MasterCashController extends CommonController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '财务管理';
        $this->breadcrumb2 = '提现管理';
        $this->url = U('MasterMaintain/index');
        $this->sqlname='ratemanage'.session('user_auth')['uid'];
    }

    public function index(){
        $time1=I('post.time1','','strtotime');
        $time2=I('post.time2','','strtotime');
        $cash_id=I('post.cash_id');
        $master_id=I('post.master_id');
        $master_name=I('post.master_name');

        //构建sql语句
        $list=M('cash c')
            ->join('left join zsf_user u on c.user_id=u.user_id')
            ->join('left join zsf_bank_card bc on bc.id=c.bcid')
            ->join('left join zsf_user_real_info ifo on ifo.user_id=c.user_id')
            ->join('left join zsf_bank_list bl on bl.bank_id=bc.bank_id');

        //判断搜索条件
        if($time1 || $time2){
            $where['c.add_time']=array('between',array($time1,$time2));
            $list=$list->where($where);
        }

        if($cash_id){
            $where['c.id']=$cash_id;
            $list=$list->where($where);
        }
        //var_dump($cash_id);exit;
        if($master_id){
            $where['c.user_id']=$master_id;
            $list=$list->where($where);
        }

        if($master_name){
            $where['ifo.id_name']=array('like',"%$master_name%");
            $list=$list->where($where);
        }


       $count=$list->count();

        $Page       = new \Think\Page($count,25);

             //记录sql语句
            $sql=M('cash c')
                ->join('left join zsf_user u on c.user_id=u.user_id')
                ->join('left join zsf_bank_card bc on bc.id=c.bcid')
                ->join('left join zsf_user_real_info ifo on ifo.user_id=c.user_id')
                ->join('left join zsf_bank_list bl on bl.bank_id=bc.bank_id');
       //判断是否带查询条件
        if(!empty($where)){
            $sql->where($where);
        }
        $sql=$sql->field('c.id,u.phone,ifo.id_name,c.money,bc.name,bc.bank_num,c.add_time,c.update_time,c.number,bl.bank_name')
            ->limit($Page->firstRow.','.$Page->listRows)->select(false);

        //查询数据列表
        $lists=M('cash c')
            ->join('left join zsf_user u on c.user_id=u.user_id')
            ->join('left join zsf_bank_card bc on bc.id=c.bcid')
            ->join('left join zsf_user_real_info ifo on ifo.user_id=c.user_id')
            ->join('left join zsf_bank_list bl on bl.bank_id=bc.bank_id');
        //判断是否带查询条件
        if(!empty($where)){
            $lists->where($where);
        }
        $lists=$lists->field('c.id,u.phone,ifo.id_name,c.money,bc.name,bc.bank_num,c.add_time,c.update_time,c.number,bl.bank_name')
            ->limit($Page->firstRow.','.$Page->listRows)->select();

        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname,$sql);

        $show       = $Page->show();
        $this->assign('list',$lists);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }


    public function add(){
        $date=I('post.time');
        $number=I('post.number');
        $id=I('post.id');

        if(!$date){
            echo 1;exit;    //日期不能为空
        }

        if(!$number){
            echo 2;exit;     //流水编号不能为空
        }

        $where['id']=$id;
        $map['update_time']=$date;
        $map['number']=$number;
        $map['status']=1;
        M('cash')->where($where)->save($map);
        echo 3;exit;
    }


    //excel导出
    public function excelout(){
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname);
        //设置列名称
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'提现账单号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'师傅账号','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'师傅名称','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'提现金额','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'到账到户','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'申请时间','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'转账编号','size'=>'20'),
            'I'=>array('index'=>'I1','name'=>'转账时间','size'=>'20'),
        );
        //设置标题
        $title='师傅提现管理';
        //选择类型
        $type=7;
        //执行sql
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;
    }
}